package com.sinosoft.web.easyui.api;

import cn.com.sinux.spring.utils.PageUtils;
import com.sinosoft.hydra.um.LoginUser;
import com.sinosoft.hydra.um.condition.SearchImportReportCondition;
import com.sinosoft.hydra.um.domain.model.ImportTemplate;
import com.sinosoft.hydra.um.domain.permission.SysUserMember;
import com.sinosoft.hydra.um.domain.permission.TImportReport;
import com.sinosoft.hydra.um.helper.LoggerHelper;
import com.sinosoft.hydra.um.services.component.dataimport.ImportParam;
import com.sinosoft.hydra.um.services.component.dataimport.ImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 导入通用api
 */
@Controller
@RequestMapping("api/dataImport")
public class SysDataImportApi {

    @Autowired
    private ImportService importService;

    /**
     * 到我能看到的部门的导入模板列表页
     *
     * @return String 页面名称
     */
    @RequestMapping(value = "doImport", method = RequestMethod.POST)
    @ResponseBody
    public Object doImport(@RequestParam(value = "keyCode", required = true) String key,
                           @RequestParam(value = "filePath", required = true) String filePath,
                           @RequestParam(value = "fileName", required = true) String fileName,
                           HttpServletRequest request,
                           LoginUser loginUser) throws Exception {
        TImportReport report = importService.doImport(key, fileName, filePath,
                new ImportParam(loginUser, request.getParameterMap()));
        SysUserMember sysUserMember = new SysUserMember();
        sysUserMember.setUserName(loginUser.getUserName());
        report.setImportUser(sysUserMember);
        ImportTemplate importTemplate = ImportTemplate.valueOf(key);
        LoggerHelper.logInfo("进行了{}数据的导入，导入文件{}", importTemplate.getTitle(), fileName);
        return report;
    }

    /**
     * 分页查询导入记录
     *
     * @param key       编码
     * @param pageSize  分页数
     * @param pageIndex 单页条数
     * @return 历史对象
     */
    @RequestMapping("histories")
    @ResponseBody
    public Object histories(@RequestParam(value = "keyCode", required = true) String key,
                            @RequestParam(value = "rows", required = false) String pageSize,
                            @RequestParam(value = "page", required = false) String pageIndex) {
        Pageable pageable = PageUtils.buildPageRequest(Integer.parseInt(pageIndex),
                Integer.parseInt(pageSize), "importTime", "desc");
        SearchImportReportCondition searchImportReportCondition = new SearchImportReportCondition();
        searchImportReportCondition.setImportKey(key);
        return importService
                .findImportReportsByKeyCode(searchImportReportCondition, pageable);
    }

    /**
     * 到我能看到的部门的导入模板列表页
     *
     * @return String 页面名称
     */
    @RequestMapping("importPanel")
    public String importConfig(@RequestParam(value = "keyCode", required = true) String key,
                               Model model) throws Exception {
        model.addAttribute("config", importService.buildConfig(key));
        model.addAttribute("keyCode", key);
        return "hydra_easyui/dataimport/panel";
    }
}
